package com.example.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.Admin;
import com.example.elmspringboot.domain.Business;
import com.example.elmspringboot.service.BusinessService;
import com.example.elmspringboot.service.Impl.BusinessServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Tag(name = "BusinessController", description = "商家食品管理")
@RestController
@RequestMapping("/api/businesses")
public class BusinessController {
    @Resource
    private BusinessService businessService;

    @Operation(summary = "获取商家列表",description = "需要登录后访问")
    @GetMapping
    public Result getAllBusiness(){
        return new Result(true,businessService.list());
    }
    @Operation(summary = "添加商家列表",description = "需要登录后访问")
    @PostMapping
    public Result saveBusiness(@RequestBody Business business){
        boolean flag = businessService.save(business);
        return new Result(flag,null,flag?"添加成功":"添加失败");
    }
    @Operation(summary = "删除商家列表",description = "需要登录后访问")
    @DeleteMapping("/{id}")
    public  Result deleteBusiness(@PathVariable Integer id){
        boolean flag = businessService.removeById(id);
        return new Result(flag,null,flag ? "删除成功":"删除失败");
    }
    @Operation(summary = "更新商家列表",description = "需要登录后访问")
    @PutMapping
    public  Result updateBusiness(@RequestBody Business business){
        boolean flag = businessService.updateById(business);
        return new Result(flag,null,flag ? "修改成功":"修改失败");
    }

    //功能实现了，传来的id为null时,读取了全部business的信息，直接进入上面的getAllBusiness里,验证成功
    @Operation(summary = "按id获取商家列表",description = "需要登录后访问")
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id){
        return new Result(true,businessService.getById(id));
    }

    //按名查找
    //当输入的参数为null时，查询出全部
    @Operation(summary = "获得所有用户",description = "需要登录后访问")
    @PostMapping("/name")
    public Result getBusinessById(@RequestParam String name) {
        List<Business> list = businessService.selectBusinessByName(name);
        Boolean flag=false;
        if(list.size()>0){
            flag=true;
        }
        return new Result(flag,list);
    }



    @Operation(summary = "获取商家列表",description = "按页展示")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize ,Business business){
        IPage<Business> page = businessService.getPage(currentPage, pageSize,business);
        if(currentPage>page.getPages()){
            page=businessService.getPage((int)page.getPages(),pageSize,business);
        }
        return new Result(true,page);
    }



    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result login(@RequestParam String username,@RequestParam String password) {
        Boolean flag = businessService.login(username, password);
        String msg="fail";
        if(flag){
            LambdaQueryWrapper<Business> wrapper=new LambdaQueryWrapper<>();
            Business business = businessService.getOne(wrapper.eq(username != null, Business::getName, username));
            msg="success";
            return new Result(true,business,msg);
        }
        return new Result(false,null,msg);
    }
}
